package com.example.hzq.zqb.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.bean.GoodsBean;
import com.example.hzq.zqb.model.Model;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/14.
 */

public class FqzsAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<GoodsBean> list;//保存要显示的数据
    private String type;
    public FqzsAdapter(Context context,String type) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
        this.type=type;
    }
    public void setList(List<GoodsBean> list){
        this.list=list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name,monthpay,price,time;
        private ImageView pic;
        public ViewHolder(View itemView) {
            super(itemView);
            pic = (ImageView) itemView.findViewById(R.id.fqzs_pic);
            name = (TextView) itemView.findViewById(R.id.fqzs_goodname);
            monthpay = (TextView) itemView.findViewById(R.id.fqzs_monthpay);
            price = (TextView) itemView.findViewById(R.id.fqzs_price);
            time = (TextView) itemView.findViewById(R.id.fqzs_time);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.fqzsitem, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final GoodsBean goodsBean = list.get(position);
        if (goodsBean == null)
            return;
        final ViewHolder viewholder = (ViewHolder) holder;
        Picasso.with(context).load(imageurl+ goodsBean.getImg()).placeholder(R.drawable.ic_default_adimage).into(viewholder.pic);
        viewholder.name.setText(goodsBean.getName());
        viewholder.monthpay.setText("每月需要支付"+Integer.valueOf(goodsBean.getPrice())/Integer.valueOf(goodsBean.getDate())+"元");
        viewholder.price.setText("还需支付"+goodsBean.getPrice()+"元");
        viewholder.time.setText(goodsBean.getCreatetime());
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("add")){
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("提示");
                    builder.setMessage("是否添加？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Model model=new Model();
                            Call<String> call=model.changeStatus(Integer.valueOf(goodsBean.getAdd_id()));
                            call.enqueue(new Callback<String>() {
                                @Override
                                public void onResponse(Call<String> call, Response<String> response) {
                                    if (response.body().toString().equals("success")){
                                        list.remove(position);
                                        notifyDataSetChanged();
                                    }
                                }

                                @Override
                                public void onFailure(Call<String> call, Throwable t) {

                                }
                            });
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();
                }
                else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("提示");
                    builder.setMessage("是否回滚？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Model model=new Model();
                            Call<String> call=model.revert();
                            call.enqueue(new Callback<String>() {
                                @Override
                                public void onResponse(Call<String> call, Response<String> response) {
                                    if (response.body().toString().equals("success")){
                                        list.clear();
                                        notifyDataSetChanged();
                                    }
                                }

                                @Override
                                public void onFailure(Call<String> call, Throwable t) {

                                }
                            });
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        return list.size();
    }
}
